//
//  RightViewController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/20.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "RightViewController.h"

#import "RightModel.h"
#import "RightViewModel.h"
#import "UIImageView+WebCache.h"
#import "CellDetailsViewController.h"

@interface RightViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)RightViewModel *rightmodel;

-(void)registerObserver;
-(void)unregiseterOberver;

@end

@implementation RightViewController
-(void)dealloc{
    
    [self unregiseterOberver];
}

- (void)getData123
{
    [self.rightmodel getData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self registerObserver];
    
    [self.view addSubview:self.tableView];
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(75, 0, 300, 75)];
    
    label.text = @"收藏列表";
    
    label.backgroundColor = [UIColor whiteColor];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.font = [UIFont systemFontOfSize:20];
    
    [self.view addSubview:label];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.0];
   
}
#pragma  mark -- getter
-(RightViewModel *)rightmodel{
    
    
    if (!_rightmodel) {
        _rightmodel = [[RightViewModel alloc]init];
    }
    return _rightmodel;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView =({
            
            UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(75, 75, 300, CGRectGetMaxY(self.view.frame)) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            [tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
            
            tableview.rowHeight = 80;
            
            tableview;
            
        });
    }
    
    
    return _tableView;
    
    
    
    
    
}
#pragma mark -- UITableViewDataSource
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellDetailsViewController *CDVC = [[CellDetailsViewController alloc]init];
    
    CDVC.IDstring = [self.rightmodel IdOftheKnowledge:indexPath];
    
    [self.navigationController pushViewController:CDVC animated:YES];
    
//    [tableView setAccessibilityElementsHidden:NO];

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.rightmodel numberOfRows];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.textColor  = [UIColor colorWithRed:1.0 green:0.6031 blue:0.5185 alpha:1.0];
    cell.textLabel.text = [self.rightmodel cellTitleAtIndexPath:indexPath];
//    NSLog(@"%@",[self.rightmodel cellTitleAtIndexPath:indexPath]);
    
    return cell;
    
    
    
    
}

#pragma mark -- private methods
-(void)registerObserver{
    
    [self.rightmodel addObserver:self forKeyPath:[self.rightmodel observeringKeyPath] options:NSKeyValueObservingOptionNew context:nil];
}
-(void)unregiseterOberver{
    [self.rightmodel removeObserver:self forKeyPath:[self.rightmodel observeringKeyPath]];
}
#pragma mark -- observer
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    if ([self.rightmodel isSuccess]) {
        
      
        [self.tableView reloadData];
    }else{
        
    }
    
    }
@end
